package com.txby.pf.modules.job.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jck.tools.ChkUtil;
import com.txby.pf.common.annotation.SysLog;
import com.txby.pf.common.utils.Constant;
import com.txby.pf.common.utils.PageUtils;
import com.txby.pf.common.utils.Query;
import com.txby.pf.common.utils.R;
import com.txby.pf.common.validator.ValidatorUtils;
import com.txby.pf.modules.job.service.impl.StationNotesJobService;
import com.txby.pf.modules.sys.controller.AbstractController;
import com.txby.pf.modules.sys.entity.SysUserEntity;
import com.txby.platformCore.entity.StationNotes;

/**
 * 定时任务提醒
 */
@RestController
@RequestMapping("/sys/stationNotes")
public class StationNotesJobController extends AbstractController{
	@Autowired
	private StationNotesJobService stationNotesJobService;
	
	/**
	 * 定时任务列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:remind:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<StationNotes> jobList = stationNotesJobService.queryList(query);
		int total = stationNotesJobService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(jobList, total, query.getLimit(), query.getPage());
		return R.ok().put("page", pageUtil);
	}
	
	/**
	 * 定时任务信息
	 */
	@RequestMapping("/info/{jobId}")
	@RequiresPermissions("sys:remind:info")
	public R info(@PathVariable("jobId") Long jobId){
		StationNotes schedule = stationNotesJobService.queryObject(jobId);
		
		return R.ok().put("schedule", schedule);
	}
	/**
	 * 查询床号自己组织初始化床号
	 */
	@RequestMapping("/queryBedNOList")
	public R queryBedNOList(@RequestParam Long deptId,@RequestParam Long sickAreaNO){
		List queryBedNOList=stationNotesJobService.queryBedNOList(deptId,sickAreaNO);
		return R.ok().put("bedNOList", queryBedNOList);
	}
	
	/**
	 * 保存定时任务
	 */
	@SysLog("保存定时任务")
	@RequestMapping("/save")
	@RequiresPermissions("sys:remind:save")
	public R save(@RequestBody StationNotes scheduleJob){
		ValidatorUtils.validateEntity(scheduleJob);
		SysUserEntity ee=getUser();
//		remindTask
		if(getUserId()== Constant.SUPER_ADMIN){
			if(ChkUtil.isEmpty(scheduleJob.getDeptId())){
				if(!ChkUtil.isEmpty(getDeptId())){
					scheduleJob.setDeptId(getDeptId().toString());
					
				}else{
					scheduleJob.setDeptId(String.valueOf(0));
				}
			}
		}else{
			if(ChkUtil.isEmpty(scheduleJob.getDeptId())){
				scheduleJob.setDeptId(getUserId().toString());
			}
		}
		scheduleJob.setCreater(getUserId().toString());
		scheduleJob.setUpdater(getUserId().toString());
		stationNotesJobService.save(scheduleJob);
		
		return R.ok();
	}
	
	/**
	 * 修改定时任务
	 */
	@SysLog("修改定时任务")
	@RequestMapping("/update")
	@RequiresPermissions("sys:remind:update")
	public R update(@RequestBody StationNotes scheduleJob){
		ValidatorUtils.validateEntity(scheduleJob);
		scheduleJob.setUpdater(getUserId().toString());	
		scheduleJob.setUpdateTime(new Date());
		stationNotesJobService.update(scheduleJob);
		return R.ok();
	}
	
	/**
	 * 删除定时任务
	 */
	@SysLog("删除定时任务")
	@RequestMapping("/delete")
	@RequiresPermissions("sys:remind:delete")
	public R delete(@RequestBody Long[] jobIds){
		stationNotesJobService.deleteBatch(jobIds);
		
		return R.ok();
	}
	
	/**
	 * 立即执行任务
	 */
	@SysLog("立即执行任务")
	@RequestMapping("/run")
	@RequiresPermissions("sys:remind:run")
	public R run(@RequestBody Long[] jobIds){
		stationNotesJobService.run(jobIds);
		
		return R.ok();
	}
	
	/**
	 * 暂停定时任务
	 */
	@SysLog("暂停定时任务")
	@RequestMapping("/pause")
	@RequiresPermissions("sys:remind:pause")
	public R pause(@RequestBody Long[] jobIds){
		stationNotesJobService.pause(jobIds);
		
		return R.ok();
	}
	
	/**
	 * 恢复定时任务
	 */
	@SysLog("恢复定时任务")
	@RequestMapping("/resume")
	@RequiresPermissions("sys:schedule:resume")
	public R resume(@RequestBody Long[] jobIds){
		stationNotesJobService.resume(jobIds);
		
		return R.ok();
	}

}
